package com.teamone.etl.systemsettings.controller;

import com.baomidou.mybatisplus.extension.api.ApiController;
import com.baomidou.mybatisplus.extension.api.R;
import com.teamone.etl.dto.LoginClass;
import com.teamone.etl.systemsettings.aspect.NoAspect;
import com.teamone.etl.systemsettings.dto.UserformDTO;
import com.teamone.etl.systemsettings.entity.Userform;
import com.teamone.etl.systemsettings.service.UserformService;
import org.checkerframework.checker.units.qual.A;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;

/**
 * (Userform)表控制层
 *
 * @author makejava
 * @since 2021-01-21 09:58:21
 */
@RestController
@RequestMapping("userform")
public class UserformController extends ApiController {
    /**
     * 服务对象
     */
    @Resource
    private UserformService userformService;

    @Resource
    private RedisTemplate<String,HashMap> redisTemplate;
    @Resource
    private HttpServletRequest request;

    /**
     * 存储session 信息
     */
    public static final String ADMIN_ID = "admin-ID";

    /**
     * 单条数据查询
     *
     * @param id 通过id进行查询
     * @return 单条数据
     */
    @GetMapping("/selectOne")
    public R selectOne(Integer id) {
        return success(userformService.selectOne(id));
    }

    /**
     *创建用户
     *
     * @param userform 接收对象
     * @return true
     */
    @PostMapping("/createUserform")
    public R createUserform(@RequestBody Userform userform) {
        Boolean userform1 = userformService.createUserform(userform);

        return success(userform1);
    }

    /**
     * 修改密码
     *
     * @param id 当前对象id
     * @param password 当前对象密码
     * @return 成功 返回 对象 失败返回false
     */
    @PutMapping("/updateUserformPwd")
    public R updateUserformPwd(@RequestParam(value = "id")Integer id,
                               @RequestParam(value = "password")String password) {

        Boolean aBoolean = userformService.updateUserformPwd(id, password);


        HashMap<String, String> stringStringHashMap = redisTemplate.opsForValue().get(ADMIN_ID);
        String username = stringStringHashMap.get("username");

        if(aBoolean){
            return success(selectOne(id));
        }
        return success(false);
    }

    /**
     *用户登录
     *
     * @param loginClass 接收对象
     * @return true false
     */
    @PostMapping("/loginUser")
    @NoAspect
    public R loginUser(@RequestBody LoginClass loginClass) {
        Userform user = userformService.loginUser(loginClass);
        if (user!=null) {
            request.getSession().setAttribute(ADMIN_ID,user);

            HashMap<String,String> hashUser = new HashMap<String,String>(16);
            hashUser.put("id",user.getId().toString());
            hashUser.put("username",user.getUsername());
            hashUser.put("name",user.getName());
            hashUser.put("iphone_number",user.getIphoneNumber());
            hashUser.put("did",user.getDid().toString());
            hashUser.put("pid",user.getPid().toString());
            hashUser.put("status",user.getStatus().toString());
            hashUser.put("createTime",user.getCreateTime().toString());
            hashUser.put("updateTime",user.getUpdateTime().toString());
            hashUser.put("createMan",user.getCreateMan().toString());
            hashUser.put("password",user.getPassword());
            hashUser.put("sessionId",request.getSession().getId());

            redisTemplate.opsForValue().set(ADMIN_ID,hashUser);
            System.out.println("userformcontroller-session-id-"+request.getSession().getId());


            return success(true);
        }
        //用户名或密码错误
        return success(false);
    }


    /**
     * 修改部门
     *
     * @param id 员工id
     * @param did 部门编号
     * @param pid 职位编号
     * @return true false
     */
    @PutMapping("/updatePostByUser")
    public R updatePostByUser(@RequestParam(value = "id") Integer id,
                              @RequestParam(value = "did") Integer did,
                              @RequestParam(value = "pid") Integer pid) {
        Boolean aBoolean = userformService.updatePostByUser(id, did, pid);

        return success(aBoolean);
    }

    /**
     * 删除员工
     *
     * @param id 员工id
     * @return true false
     */
    @DeleteMapping("/deleteUserById")
    public R deleteUserById(Integer id) {

        Boolean aBoolean = userformService.deleteUserById(id);

        return success(aBoolean);
    }

    /**
     * 修改员工信息
     *
     * @param userformDTO 传输对象
     * @return true  false
     */
    @PutMapping("/updateUser")
    public R updateUser(@RequestBody UserformDTO userformDTO) {
        Boolean aBoolean = userformService.updateUser(userformDTO);
        return success(aBoolean);
    }
}